﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Zenntrix_Toolkit;
using Xentrix;

namespace Vara_Data_Layer.Vara_Data_Categories
{
    public class Vara_Data_Category_Controller : Vara_Data_ICategory_Actions
    {
        private static List<Vara_Data_ICategory> _categories;
        public List<Vara_Data_ICategory> Categories
        {
            get
            {
                if (_categories == null)
                    Refresh_Category_List();
                return _categories;
            }
        }

        public void Refresh_Category_List()
        {
            DataTable DataTableCategories;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableCategories = NewSqlConnector.ExecuteProcedure("Return_Categories");
            }
            List<Vara_Data_ICategory> ListOfCategories = new List<Vara_Data_ICategory>();
            foreach (DataRow R in DataTableCategories.Rows)
            {
                Vara_Data_ICategory CategoryToAppend = new Vara_Data_Category(int.Parse(R["ID"].ToString()));
                CategoryToAppend.Name = R["Name"].ToString();
                ListOfCategories.Add(CategoryToAppend);
            }
            _categories = ListOfCategories;
        }

        public Vara_Data_ICategory ReturnCategory(int ID)
        {
            Vara_Data_ICategory CategoryToReturn;
            CategoryToReturn = Categories.Find(delegate(Vara_Data_ICategory Category) { return Category.ID == ID; });
            return CategoryToReturn;
        }
    }
}
